﻿using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PDFTool
{
    public partial class FrmMain : Form
    {
        private int totalPage = 0;
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            var ofg = new OpenFileDialog();
            ofg.Filter = "*.pdf|*.pdf";

            if (ofg.ShowDialog() == DialogResult.OK)
            {
                lblPDF.Text = ofg.FileName;

                var pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(lblPDF.Text);
                totalPage = pdfDoc.PageCount;
                pdfDoc.Close();

                lblTotalPage.Text = "共" + totalPage + "页";

                listView1.Items.Clear();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lblPDF.Text.Trim().Length == 0)
            {
                MessageBox.Show("请先选择PDF文件");
                return;
            }

            FrmSplit frm = new FrmSplit();
            frm.TotalPage = totalPage;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                int start = frm.StartPage;
                int end = frm.EndPage;
                string file = frm.FileName;

                ListViewItem item = new ListViewItem();
                item.Text = start + "-" + end;
                item.SubItems.Add(file);

                listView1.Items.Add(item);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                listView1.Items.Remove(listView1.SelectedItems[0]);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            var bfg = new FolderBrowserDialog();

            if (bfg.ShowDialog() == DialogResult.OK)
            {
                string path = bfg.SelectedPath;

                var pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(lblPDF.Text, PdfDocumentOpenMode.Import);

                foreach (ListViewItem item in this.listView1.Items)
                {
                    int start = Convert.ToInt32(item.Text.Split("-".ToCharArray())[0]);
                    int end = Convert.ToInt32(item.Text.Split("-".ToCharArray())[1]);
                    string new_file = System.IO.Path.Combine(path, item.SubItems[1].Text + ".pdf");

                    var new_pdf = new PdfDocument();
                    new_pdf.Version = pdfDoc.Version;
                    for (int i = start - 1; i <= end - 1; i++)
                    {
                        new_pdf.Pages.Add(pdfDoc.Pages[i]);
                    }
                    new_pdf.Save(new_file);
                    new_pdf.Close();
                }

                pdfDoc.Close();

                MessageBox.Show("导出完成！");
            }

        }
    }
}
